package com.example.administrator.student4;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/13.
 */
public class StudentDetail extends Activity {
    EditText studentnumber,name,age;
    Spinner sex,classnumber;
    Button comfirm;
    String[] arrsex = {"boy","girl"},arrclass;
    MyHandler myHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studentdetail);
        uiinit();
        myHandler = new MyHandler();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    arrclass = WebUtil.classfind();
                    myHandler.sendEmptyMessage(0x111);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        comfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final JSONObject object = new JSONObject();

                try {
                    object.put("classnumber",classnumber.getSelectedItem().toString());
                    object.put("studentnumber",studentnumber.getText().toString());
                    object.put("sex",sex.getSelectedItem().toString());
                    object.put("age",age.getText().toString());
                    object.put("name",name.getText().toString());
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            String path = "http://10.0.2.2:8080/andorid/student?request=add";
                            URL url = null;
                            try {
                                url = new URL(path);
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                WebUtil.jsonOut(object,connection);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    Toast.makeText(StudentDetail.this,"成功添加",Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void uiinit(){
        studentnumber = (EditText) findViewById(R.id.studentnumber);
        name = (EditText) findViewById(R.id.name);
        age = (EditText) findViewById(R.id.age);
        sex = (Spinner) findViewById(R.id.sex);
        classnumber = (Spinner) findViewById(R.id.classnumber);
        comfirm = (Button) findViewById(R.id.comfirm);

    }
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0x111){
                ArrayAdapter<String> apsex = new ArrayAdapter<String>(StudentDetail.this,android.R.layout.simple_expandable_list_item_1,arrsex);
                ArrayAdapter<String> apclass = new ArrayAdapter<String>(StudentDetail.this,android.R.layout.simple_expandable_list_item_1,arrclass);
                sex.setAdapter(apsex);
                classnumber.setAdapter(apclass);
            }
        }
    }
}
